#!/usr/bin/env python

import re
import sys

from glob import glob

try:
    from urllib import quote_plus
except ImportError:
    from urllib.parse import quote_plus


_RE_COMMENT = re.compile(r"^[ \t]*#")



TRACKERS = ""
with open("trackers.txt") as f:
    while True:
        line = f.readline()

        if not line:
            break  # EOF

        line = line.strip()

        if not line:
            continue  # Skip empty line

        if _RE_COMMENT.match(line):
            continue  # Skip comment

        TRACKERS += ("&tr=" + quote_plus(line))


for patch in glob("templates/transmission-*.js.patch"):
    with open(patch) as f, open(patch.replace("templates/", ""), "w") as f2:
        template = f.read()
        f2.write(template.replace("@!trackers!@", TRACKERS))
